
import { InjectRepository } from '@nestjs/typeorm';
import { Repository ,Not ,In,Like} from 'typeorm';
import { Injectable } from '@nestjs/common';
import { ResultData } from 'src/common/utils/result';
import { CreateClassDto, UpdateClassDto, QueryClassDto } from './dto/class.dto';
import { ClassEntity } from './entities/class.entity';

@Injectable()
export class ClassService {
  constructor(
      @InjectRepository(ClassEntity)
      private readonly classEntityRep: Repository<ClassEntity>,
  ) {}
  async create(createClassDto: CreateClassDto) {
    const res = await this.classEntityRep.save(createClassDto);
    return ResultData.ok(res);
  }

  // 数据处理函数
  private  returnFormatDate(data) {
    return data.map(item=>({
      ...item,
      schoolName: item.school?.schoolName,
      proName: item.professional?.proName,
      campusName: item.campus?.campusName,
    }))
  }

  async findAll(query :QueryClassDto ) {
    const entity = this.classEntityRep.createQueryBuilder('entity');

    entity.leftJoinAndSelect('entity.school', 'school')
    entity.leftJoinAndSelect('entity.campus', 'campus')
    entity.leftJoinAndSelect('entity.professional', 'professional')

    entity.where({ delFlag: '0'});
    if(query.classId){
      entity.andWhere("entity.classId = :classId", {classId: query.classId});
    }
    if(query.delFlag){
      entity.andWhere("entity.delFlag = :delFlag", {delFlag: query.delFlag});
    }
    if(query.status){
      entity.andWhere("entity.status = :status", {status: query.status});
    }
    if(query.createBy){
      entity.andWhere("entity.createBy = :createBy", {createBy: query.createBy});
    }
    if(query.params?.beginTime && query.params?.endTime){
      entity.andWhere("entity.createTime BETWEEN :start AND :end", { start: query.params.beginTime, end: query.params.endTime });
    }
    if(query.updateBy){
      entity.andWhere("entity.updateBy = :updateBy", {updateBy: query.updateBy});
    }
    if(query.params?.beginTime && query.params?.endTime){
      entity.andWhere("entity.updateTime BETWEEN :start AND :end", { start: query.params.beginTime, end: query.params.endTime });
    }
    if(query.remark){
      entity.andWhere("entity.remark = :remark", {remark: query.remark});
    }
    if(query.className){
      entity.andWhere("entity.className LIKE :className", {className: `%${query.className}%`});
    }
    if(query.entryDate){
      entity.andWhere("entity.entryDate = :entryDate", {entryDate: query.entryDate});
    }
    if(query.schoolId){
      entity.andWhere("entity.schoolId = :schoolId", {schoolId: query.schoolId});
    }
    if(query.campusId){
      entity.andWhere("entity.campusId = :campusId", {campusId: query.campusId});
    }
    if(query.managerId){
      entity.andWhere("entity.managerId = :managerId", {managerId: query.managerId});
    }
    if(query.proId){
      entity.andWhere("entity.proId = :proId", {proId: query.proId});
    }


    if (query.orderByColumn && query.isAsc) {
      const key = query.isAsc === 'ascending' ? 'ASC' : 'DESC';
      entity.orderBy(`entity.${query.orderByColumn}`, key);
    }

    if(!query.pageNum || !query.pageSize){
      const list = await entity.getMany();
      return ResultData.ok(this.returnFormatDate(list));
    }

    entity.skip(query.pageSize * (query.pageNum - 1)).take(query.pageSize);
    const [list, total] = await entity.getManyAndCount();
    return ResultData.ok({
      list:this.returnFormatDate(list),
      total,
    });
  }

  async findOne(id: number) {
    const res = await this.classEntityRep.findOne({
      where: {
        delFlag: '0',
        classId: id,
      },
    });
    return ResultData.ok(res);
  }

  async update(updateClassDto: UpdateClassDto) {
    const res = await this.classEntityRep.update({  classId: updateClassDto.classId }, updateClassDto);
    return ResultData.ok({value:res.affected >= 1});
  }

  async remove(classIds: number[]) {
    const res = await this.classEntityRep.update(
        { classId: In(classIds) },
        {
          delFlag: '1',
        },
    );
    return ResultData.ok({value:res.affected >= 1});
  }
}
